﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace DnnDash.SimplePhoneApp.Common
{
    internal class Helper
    {
        internal static Uri GetUri(string domainAlias, string modulePath, string controllerName, string callName, bool secure)
        {
            string scheme = "";
            if (secure)
                scheme = Uri.UriSchemeHttps + Uri.SchemeDelimiter;
            else
                scheme = Uri.UriSchemeHttp + Uri.SchemeDelimiter;

            Uri uri = new Uri(string.Format("{0}{1}/DesktopModules/{2}/API/{3}/{4}", scheme, domainAlias, modulePath, controllerName, callName));

            return uri;
        }

        internal static HttpWebRequest GetRequest(Uri uri, AppSettings creds)
        {
            // initialize a new WebRequest
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            // set cookies
            //if (creds.Cookies != null)
            //    request.CookieContainer = creds.Cookies;

            // set credentials
            request.Credentials = new NetworkCredential(creds.Username, creds.Password);

            return request;
        }

        internal static Version GetCurrentVersion()
        {
            Version version;

            var assembly = Assembly.GetExecutingAssembly().FullName;
            var fullVersionNumber = assembly.Split('=')[1].Split(',')[0];

            version = new Version(fullVersionNumber);

            return version;
        }

        internal static refType DeSerializer<refType>(XElement element)
        {
            var serializer = new XmlSerializer(typeof(refType));
            return (refType)serializer.Deserialize(element.CreateReader());
        }

    }
}
